package com.whxy02.tolearn.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whxy02.tolearn.app.beans.ExamCollection;
import com.whxy02.tolearn.app.dao.ExamCollectionDao;
import com.whxy02.tolearn.app.service.ExamCollectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExamCollectionServiceImpl implements ExamCollectionService {
    private QueryWrapper<ExamCollection> wrapper=new QueryWrapper<>();
    @Resource
    ExamCollectionDao examCollectionDao;

    @Override
    public List<ExamCollection> getAll() throws Exception {
        return examCollectionDao.selectList(null);
    }

    @Override
    public ExamCollection getById(Integer id) throws Exception {
        return examCollectionDao.selectById(id);
    }

    @Override
    public int save(ExamCollection examCollection) throws Exception {
        return examCollectionDao.insert(examCollection);
    }

    @Override
    public int removeById(Integer id) throws Exception {
        return examCollectionDao.deleteById(id);
    }

    @Override
    public int change(ExamCollection examCollection) throws Exception {
        return examCollectionDao.updateById(examCollection);
    }

    @Override
    public ExamCollection getByUserAndExam(Integer userId, Integer examId) throws Exception {
        wrapper.clear();
        wrapper.eq("exam_id",examId).eq("user_id",userId);

        return examCollectionDao.selectOne(wrapper);
    }

    @Override
    public Page<ExamCollection> getByUserIdAndDateDescPage(Integer userId, int nowPage) throws Exception {
        wrapper.clear();
        wrapper.eq("user_id",userId).orderByDesc("create_time");
        Page<ExamCollection> page=new Page<>(nowPage,20);
        return examCollectionDao.selectPage(page,wrapper);
    }
}
